package net.xiduth.game.npc.glacor;

import net.xiduth.game.Graphics;
import net.xiduth.game.Hit;
import net.xiduth.game.Hit.HitLook;
import net.xiduth.game.World;
import net.xiduth.game.WorldTile;
import net.xiduth.game.npc.NPC;
import net.xiduth.game.npc.TimerBar;
import net.xiduth.game.player.Player;
import net.xiduth.game.tasks.WorldTask;
import net.xiduth.game.tasks.WorldTasksManager;
import net.xiduth.utils.Utils;

public class UnstableMinion extends NPC {

	private static final long serialVersionUID = 6520218487867695493L;
	
	public Glacor parent;
	public static boolean defeated = false;

	public boolean startedTimer = false;
	public boolean healing = false;

	public int gainedHp = 0;

	final int EXPLODE_GFX = 956;

	final NPC thisNpc = this;

	public UnstableMinion(int id, WorldTile tile, int mapAreaNameHash,
			boolean canBeAttackFromOutOfArea, boolean spawned, Glacor parent) {
		super(id, tile, mapAreaNameHash, canBeAttackFromOutOfArea, spawned);
		this.parent = parent;
	}

	@Override
	public void processEntity() {
		super.processEntity();

		if ((this.getHitpoints() < 500) && !isDead() && (getHitpoints() != 0))
			this.heal(10);

		if (!startedTimer && this.getHitpoints() > 500 && !isDead()) {
			this.setNextTimerbar(new TimerBar(750));
			startedTimer = true;
		    WorldTasksManager.schedule(new WorldTask() {
				@Override
				public void run() {
					if (thisNpc.getHitpoints() <= 0 || thisNpc.isDead())
						return;
					for (Player player : World.getPlayers()) {
						if (Utils.getDistance(thisNpc.getX(), thisNpc.getY(),
								player.getX(), player.getY()) < 2)
							player.applyHit(new Hit(player, player
									.getHitpoints() / 3, HitLook.REGULAR_DAMAGE));
					}
					thisNpc.applyHit(new Hit(thisNpc, (int) (thisNpc
							.getHitpoints() * 0.90), HitLook.REGULAR_DAMAGE));
					thisNpc.setNextGraphics(new Graphics(EXPLODE_GFX));
					startedTimer = false;
				}
			}, 25);
		}

		if (getHitpoints() <= 0 || isDead()) {
			if (!defeated)
			World.sendProjectile(this, parent, 634, 34, 16, 30, 35, 16, 15);
			defeated = true;
		}
	}

}